* gph of IQ vs. CSR for CSR in 1980, since IQ was calibrated for 1990
* 	with an average age of test-taker of 10 in Wichert 2010
* followed by avplot of TIMSS/PIRLS scores vs. csr

cd "~/Documents/econ/research/current/cognitive/estimation/data"
adopath ++ "~/Documents/econ/research/stata/ado/personal/gavplot"

/**/
use growth_panel if (year>=1960&year<=1990), clear
label var iq "IQ"
label var csr "Child Survival Rate"
label var lgdppc "log GDP per capita"
qui reg iq csr lgdppc if year==1980, robust
local oregopts "starlevels(5 1 .1) se squarebrack"
outreg, nodisplay varlabels `oregopts' bdec(3 2) ctitle("", "IQ"\ "", "")

gavplot csr, msymbol(S) msize(medlarge) mcolor(forest_green) ///
	rlopts(lcolor(maroon) lwidth(thick)) ciunder xsize(20) ysize(12.5) ///
	title(IQ and Child Survival Rate) subtitle(controlling for income) ///
	ytitle(IQ | Income) xtitle(Child Survival | Income)  ///
	ylabel(, angle(horizontal)) nocoef
graph export ../../draft/images/iq_vs_csr_slide.pdf, replace

gavplot csr, msymbol(S) msize(medsmall) mcolor(forest_green) ///
	rlopts(lcolor(maroon) lwidth(thick)) ciunder ///
	ytitle(IQ | Income) xtitle(Child Survival | Income)  ///
	ysize(3.5) xsize(5) scale(*1.1)		///
	ylabel(, angle(horizontal)) nocoef
graph export ../../draft/images/iq_vs_csr.pdf, replace
/**/
exit

// Now TIMSS/PIRLS data

use ../data/WDI, clear
sort countrycode year

local pwt "~/Documents/econ/data/macro/XCountry/PWT/pwt10/pwt100"
merge 1:1 countrycode year using "`pwt'", keep(match) keepusing(rgdpo emp pop) 
encode countrycode, generate(isonum)
xtset isonum year
gen csr = round(1000-L10.cmr,.1)
label var csr "Child Survival Rate"
gen double lgdppw = ln(L10.rgdpo/L10.emp)
label var lgdppw "log PWT GDP p.w., PWT rgdpo/emp"
gen double lgdppc = ln(L10.rgdpo/L10.pop)
label var lgdppc "log GDP per capita"
keep countrycode isonum year csr lgdppw lgdppc
sort countrycode year

merge 1:1 countrycode year using ../data/timss, keep(match)
drop _merge
label var ed_exp_pct `"Education expenditure" \ "(\% of GDP)""'
label var timssmat "TIMSS math"
label var timsssci "TIMSS science"
label var pirlsread "PIRLS reading"

local oregopts `"`oregopts' rtitle("Child Survival Rate" \ "" \ "log GDP per capita" \ "" \ "Education expenditure" \ "\ \ \ \begin{small}(\% of GDP)\end{small}" \ "Constant" \ "")"'
local comopt "xlabel(940(20)1000) ysize(4) xscale(range(930 1000)) yscale(range(250 590)) msize(small) nocoef"	// 	

qui reg timssmat csr lgdppc ed_exp_pct, ///
	vce(cluster isonum)
qui tab countrycode if e(sample)
di "No. countries = `r(r)'"
outreg, nodisplay merge `oregopts' bdec(2 1 2 0) ctitle("","TIMSS" \ "", "Math")
gavplot csr, title("Math") ytitle("TIMSS/PIRLS Score" " ")	///
	ylabel(250(100)550, nolabels) xtitle(" ") `comopt' ///
	name(Tim_mat, replace) 

qui reg timsssci csr lgdppc ed_exp_pct, vce(cluster isonum)
qui tab countrycode if e(sample)
di "No. countries = `r(r)'"
outreg, nodisplay merge `oregopts' bdec(2 1 2 0) ctitle("","TIMSS" \ "", "Science")
gavplot csr, title(Science)	ytitle("") 	///
	ylabel(250(100)550) xtitle("Child Survival Rate") `comopt' ///
	name(Tim_sci, replace) 

qui reg pirlsread csr lgdppc ed_exp_pct, vce(cluster isonum)
qui tab countrycode if e(sample)
di "No. countries = `r(r)'"
gavplot csr, title(Reading)	ytitle(" ") ///
	ylabel(250(100)550, nolabels) xtitle(" ") `comopt' ///
	name(Prl_rea, replace)
outreg using ../../draft/images/iq_timss, merge bdec(2 1 2 0) ///
	`oregopts' frag tex  ctitle("", "PIRLS" \ "", "Reading")	///
	note("National child survival rates are correlated with cognitive measures"	///
		 \ "controlling for income and education expenditure. Robust standard errors"	///
		 \ "in brackets, which for TIMSS and PIRLS data are clustered by country.")
	
graph combine Tim_mat Tim_sci Prl_rea, rows(1) 		///
	ysize(2.2) iscale(*1.3) 						///
	title(Child survival vs. 4th grade TIMSS/PIRLS scores)	///
	subtitle(conditional on income levels and education expenditure)
graph export "../../draft/images/timss_vs_csr_slide.pdf", replace

graph combine Tim_mat Tim_sci Prl_rea, rows(1) 		///
	ysize(2.2) iscale(*1.7)
graph export "../../draft/images/timss_vs_csr.pdf", replace

// regression results without ed_exp_pct
local outopts "starlevels(5 1 .1) se squarebrack bdec(2 1 0) varlabel"
qui reg timssmat csr lgdppc, vce(cluster isonum)
qui tab countrycode if e(sample)
di "No. countries = `r(r)'"
outreg, `outopts' ctitle("","TIMSS" \ "", "Math") nodisplay
qui reg timsssci csr lgdppc, vce(cluster isonum)
qui tab countrycode if e(sample)
di "No. countries = `r(r)'"
outreg, merge `outopts' ctitle("","TIMSS" \ "", "Science") nodisplay
qui reg pirlsread csr lgdppc, vce(cluster isonum)
qui tab countrycode if e(sample)
di "No. countries = `r(r)'"
outreg using ../../draft/images/timss_no_ed, merge ///
	`outopts' frag tex ctitle("", "PIRLS" \ "", "Reading")	///
	note("Standard errors clustered by country.")
